package com.situ.hospital.dao.Impl;

import com.situ.hospital.dao.IAuthDAO;
import com.situ.hospital.pojo.Auth;
import com.situ.hospital.pojo.query.AuthQuery;
import com.situ.hospital.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AuthDAOImpl implements IAuthDAO {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Auth> selectByPage(AuthQuery authQuery) {
        String sql = "SELECT id,`name`,path,role,status,create_time,update_time FROM auth WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = authQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND `name` LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryRole = authQuery.getRole();
        if(queryRole != null) {
            sql += " AND role=?";
            queryList.add(queryRole);
        }
        Date queryBeginCreateTime = authQuery.getBeginCreateTime();
        Date queryEndCreateTime = authQuery.getEndCreateTime();
        if(queryBeginCreateTime != null && queryEndCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(queryBeginCreateTime);
            queryList.add(queryEndCreateTime);
        }
        sql += " LIMIT ?,?";
        int offset = (authQuery.getPage() - 1) * authQuery.getLimit();
        queryList.add(offset);
        queryList.add(authQuery.getLimit());
        return template.query(sql, new BeanPropertyRowMapper<>(Auth.class), queryList.toArray());
    }

    @Override
    public int selectTotalCount(AuthQuery authQuery) {
        String sql = "SELECT COUNT(*) FROM auth WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = authQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND `name` LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryRole = authQuery.getRole();
        if(queryRole != null) {
            sql += " AND role=?";
            queryList.add(queryRole);
        }
        Date queryBeginCreateTime = authQuery.getBeginCreateTime();
        Date queryEndCreateTime = authQuery.getEndCreateTime();
        if(queryBeginCreateTime != null && queryEndCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(queryBeginCreateTime);
            queryList.add(queryEndCreateTime);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        String sql = "DELETE FROM auth WHERE id=?";
        template.update(sql, id);
    }

    @Override
    public void add(Auth auth) {
        String sql = "INSERT INTO auth(`name`,path,role) VALUES(?,?,?)";
        template.update(sql, auth.getName(), auth.getPath(), auth.getRole());
    }

    @Override
    public Auth selectById(int id) {
        String sql = "SELECT id,`name`,path,role,status,create_time,update_time FROM auth WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Auth.class), id);
    }

    @Override
    public void Update(Auth auth) {
        String sql = "UPDATE auth SET `name`=?,path=?,role=? WHERE id=?";
        template.update(sql, auth.getName(), auth.getPath(), auth.getRole(), auth.getId());
    }

    @Override
    public void updateStatus(int id, int status) {
        String sql = "UPDATE auth SET status=? WHERE id=?";
        template.update(sql, status, id);
    }

    @Override
    public List<Auth> selectByRole(Integer role) {
        String sql = "SELECT id,`name`,path,role,status,create_time,update_time FROM auth WHERE role=?";
        return template.query(sql, new BeanPropertyRowMapper<>(Auth.class), role);
    }
}
